package mvc.Model.Bbs;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibatis.sqlmap.client.SqlMapClient;
import common.sqlmap.GetSqlMapClient;
import common.utility.SearchDTO;

public class BbsDataDAO {
	private SqlMapClient sqlMapClient;      
	
	public BbsDataDAO() {
	    super();
	    sqlMapClient = GetSqlMapClient.get();
	}

	public int getCount(String keyColumn, String keyWord, String code) throws SQLException {
		int cnt = 0;
		
		Map map = new HashMap();
		map.put("keyColumn", keyColumn);
		map.put("keyWord", keyWord);
		map.put("bbs_code", code);
        
		cnt = (Integer) sqlMapClient.queryForObject("bbsdata.count", map);

		return cnt;
	}

	public List list(String keyColumn, String keyWord, int beginPerPage, int numPerPage, String code) throws SQLException {
		int startRow = beginPerPage + 1;
		int endRow = beginPerPage + numPerPage;
        
        Map<String, Comparable> map = new HashMap<String, Comparable>();
        
        map.put("keyColumn", keyColumn);
        map.put("keyWord", keyWord);
        map.put("startRow", startRow);
        map.put("endRow", endRow);
        map.put("bbs_code", code);
        
		return sqlMapClient.queryForList("bbsdata.list", map);
	}

	public boolean create(BbsDataDTO dto) throws SQLException {
		boolean flag = false;
		
		Object obj = null;
        try{
			sqlMapClient.startTransaction();
			obj = sqlMapClient.insert("bbsdata.create", dto);
			System.out.println(obj);
			if(obj != null){
				flag = true;
			}
			sqlMapClient.commitTransaction();
			
        }catch(Exception e){
            System.out.println(e.toString());
        }finally{
            sqlMapClient.endTransaction();
        }
        
        return flag;
	}

	public BbsDataDTO read(int bSeq, String bbsCode) {
		BbsDataDTO dto = null;
		
		Map map = new HashMap();
		map.put("b_seq", bSeq);
		map.put("bbs_code", bbsCode);
		
		try{
			dto = (BbsDataDTO)sqlMapClient.queryForObject("bbsdata.read", map);
		}catch(Exception e){
			System.out.println(e.toString());
		}
		
		return dto;
	}

	public int getB_ID(String bbsCode) {
		int b_id = 0;
		
		try{
			b_id = (Integer) sqlMapClient.queryForObject("bbsdata.getB_id", bbsCode);
		}catch(Exception e){
			System.out.println(e.toString());
		}
		
		return b_id;
	}

	public void uphit(int bSeq) {
		try{
			sqlMapClient.update("bbsdata.uphit", bSeq);
		}catch(Exception e){
			System.out.println(e.toString());
		}
	}

	public boolean update(BbsDataDTO dto) {
		boolean flag = false;
		
		Object obj = null;
        try{
			obj = sqlMapClient.update("bbsdata.update", dto);
			if(obj != null){
				flag = true;
			}
			
        }catch(Exception e){
            System.out.println(e.toString());
        }
        
        return flag;
	}

	public boolean delete(int bSeq, String bbsCode) {
		boolean flag = false;
		
		Map map = new HashMap();
		map.put("b_seq", bSeq);
		map.put("bbs_code", bbsCode);

		Object obj = null;
		try{
			obj = sqlMapClient.delete("bbsdata.remove", map);
			if(obj != null){
				flag = true;
			}
		}catch(Exception e){
			System.out.println(e.toString());
		}
		
		return flag;
	}

	public double getSubID(double bSubid, String bbsCode) {
		double b_subid = 0;
		
		Map map = new HashMap();
		map.put("b_subid", bSubid);
		map.put("bbs_code", bbsCode);
		
		try{
			b_subid = (Double) sqlMapClient.queryForObject("bbsdata.getSubid", map);
		}catch(Exception e){
			System.out.println(e.toString());
		}
		
		return b_subid;
	}

	public boolean reply(BbsDataDTO dto) throws SQLException {
		boolean flag = false;
		
		Object obj = null;
        try{
			sqlMapClient.startTransaction();
			obj = sqlMapClient.insert("bbsdata.reply", dto);
			System.out.println(obj);
			if(obj != null){
				flag = true;
			}
			sqlMapClient.commitTransaction();
			
        }catch(Exception e){
            System.out.println(e.toString());
        }finally{
            sqlMapClient.endTransaction();
        }
        
        return flag;
	}

	public List toplist(String bbsCode) throws SQLException {
		return sqlMapClient.queryForList("bbsdata.toplist", bbsCode);
	}

	public boolean sel_remove(List<Integer> arrBSeq) {
		boolean flag = false;
		Object obj = null;
		
		try{
			obj = sqlMapClient.delete("bbsdata.sel_remove", arrBSeq);
			System.out.println(obj);
			if(obj != null){
				flag = true;
			}
			
		}catch(Exception e){
			System.out.println(e.toString());
		}
		return flag;
	}


}
